﻿#define XML
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Web.SessionState;

[WebService ( Namespace = "http://tempuri.org/" )]
[WebServiceBinding ( ConformsTo = WsiProfiles.BasicProfile1_1 )]
[ScriptService]
public class timer_webservice : System.Web.Services.WebService
{

	[WebMethod ( true )]
	[ScriptMethod]
	public int NewEMailCount ( )
	{
		this.Context.Response.Cache.SetNoStore ( );

		// 返回两次调用 NewEMailCount 之间的新邮件数

		if ( null == this.Session["newemailcount"] )
			this.Session["newemailcount"] = 0;

		int count = Convert.ToInt32 ( this.Session["newemailcount"] );

		this.Session["newemailcount"] = 0;

		return count;
	}

	[WebMethod ( true )]
	[ScriptMethod]
	public SortedDictionary<string, object> GetEMailList ( int pageindex, int pagesize )
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = ( pageindex - 1 ) * pagesize + 1;
		int endIndex = pageindex * pagesize;

		SampleDS.EMail1DataTable table;

		if ( null == this.Session["email1"] )
		{
			table = new SampleDS ( ).EMail1;
			table.AddEMail1Row ( "jack@tom.com", "Hi, my friend", DateTime.Parse ( "2011-2-4" ), false );
			table.AddEMail1Row ( "lili@126.com", "Hi, my friend2", DateTime.Parse ( "2011-2-1" ), false );
			table.AddEMail1Row ( "123456789@qq.com", "play game", DateTime.Parse ( "2011-2-1" ), false );
			table.AddEMail1Row ( "jack@tom.com", "您的账户已经不能用了", DateTime.Parse ( "2011-1-21" ), false );
			table.AddEMail1Row ( "jack2@tom.com", "哈哈, 好了", DateTime.Parse ( "2011-1-10" ), false );

			this.Session["email1"] = table;
		}
		else
			table = this.Session["email1"] as SampleDS.EMail1DataTable;

		int itemcount;

		SortedDictionary<string, object> values = new SortedDictionary<string, object> ( );
		values.Add ( "rows", SampleHelper.CreateJSONArray ( SampleHelper.Filter ( null, "IsNew desc, Time desc", beginIndex, endIndex, table.DataSet as SampleDS, "EMail1", out itemcount ) ) );
		values.Add ( "itemcount", itemcount );

		/*
		 * {
		 *		"itemcount": 100,
		 *		"rows":
		 *		[
		 *			{ "sender": "xxxx", "title": ... }
		 *		]
		 * }
		 * */
		return values;
	}

	[WebMethod ( true )]
	[ScriptMethod]
	public string SendEMail ( string sender, string title )
	{
		this.Context.Response.Cache.SetNoStore ( );

		SampleDS.EMail1DataTable table = this.Session["email1"] as SampleDS.EMail1DataTable;

		table.AddEMail1Row ( sender, title, DateTime.Today, true );

		if ( null == this.Session["newemailcount"] )
			this.Session["newemailcount"] = 0;

		this.Session["newemailcount"] = Convert.ToInt32 ( this.Session["newemailcount"] ) + 1;

		return "发送 ok 了";
	}

}
